//
//  TJModifyPOIController.m
//  BaoDian
//
//  Created by mac on 2024/7/27.
//

#import "TJModifyPOIController.h"
#import "TJButton.h"
#import "QMUITextView.h"


@interface TJModifyPOIController ()<QMUITextViewDelegate>

@property (nonatomic, weak) UIView *contentView;
@property (nonatomic, weak) UIView *maskView;
@property (nonatomic, weak) TJButton *confirmButton;
@property (nonatomic, weak) QMUITextView *textView;
@property (nonatomic, weak) UIButton *checkButton;

@end

@implementation TJModifyPOIController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = UIColor.clearColor;
    
    UIButton *maskView = [[UIButton alloc] initWithFrame:self.view.bounds].addTo(self.view);
    maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    [maskView addTarget:self action:@selector(didClickMaskButton) forControlEvents:UIControlEventTouchUpInside];
    _maskView = maskView;
    
    UIView *contentView = [UIView addToView:maskView];
    contentView.backgroundColor = UIColorMake(0x1E1F23);
    _contentView = contentView;
    
    UIView *titleView = [UIView addToView:contentView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.right.offset(0);
        make.height.offset(70);
    }];
    
    UILabel *titleLabel = [UILabel lableWithSuperView:titleView font:UIFontMediumMake(16) color:UIColor.whiteColor title:@"修改POI地址" textAlignment:NSTextAlignmentLeft];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
    }];
        
    UIView *textViewContentView = [UIView addToView:contentView];
    textViewContentView.addCornerRadius(8);
    textViewContentView.backgroundColor = UIColorMake(0x2A2B31);
    [textViewContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(16);
        make.height.offset(120);
        make.top.equalTo(titleView.mas_bottom).offset(8);
    }];
 
    QMUITextView *textView = [QMUITextView addToView:textViewContentView];
    textView.font = UIFontMake(14);
    textView.backgroundColor = UIColorMake(0x2A2B31);
    textView.placeholder = @"请输入需要设置的POI地址";
    textView.delegate = self;
//    textView.returnKeyType = UIReturnKeyDone;
    textView.textColor = UIColor.whiteColor;
    textView.placeholderColor = UIColor.tj_placeholderTextColor;
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.inset(8);
        make.right.inset(8);
        make.top.bottom.offset(0);
    }];
    
    TJButton *checkButton = [TJButton buttonWithSuperView:contentView font:UIFontMake(12) color:UIColor.whiteColor title:@"校验"];
    checkButton.enabled = NO;
    checkButton.addCornerRadius(12);
    [checkButton addTarget:self action:@selector(didClickCheckButton) forControlEvents:UIControlEventTouchUpInside];
    checkButton.backgroundColor = UIColorMake(0x2978F0);
    [checkButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textViewContentView.mas_bottom).offset(12);
        make.right.equalTo(textViewContentView);
        make.width.offset(48);
        make.height.offset(24);
    }];
    
    TJButton *cancelButton = [TJButton buttonWithSuperView:contentView font:UIFontMake(16) color:UIColor.whiteColor title:@"取消"];
    [cancelButton addTarget:self action:@selector(didClickDismissButton) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.inset(TabbarSafeBottomMargin);
        make.height.offset(54);
        make.left.right.inset(0);
    }];
    
    UIView *marginView = [UIView addToView:contentView];
    marginView.backgroundColor = UIColorMake(0x2A2B31);
    [marginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(0);
        make.height.offset(8);
        make.bottom.equalTo(cancelButton.mas_top);
    }];
    
    TJButton *confirmButton = [TJButton buttonWithSuperView:contentView font:UIFontMake(16) color:UIColorMake(0x2978F0) title:@"确认修改"];
    confirmButton.addCornerRadius(12);
    confirmButton.enabled = NO;
    [confirmButton addTarget:self action:@selector(didClickConfirmButton) forControlEvents:UIControlEventTouchUpInside];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(cancelButton);
        make.bottom.equalTo(marginView.mas_top);
    }];
    
    UIView *lineView = [UIView addToView:contentView];
    lineView.backgroundColor = UIColorMake(0x2A2B31);
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(0);
        make.height.offset(1);
        make.bottom.equalTo(confirmButton.mas_top);
    }];
    
    CGFloat contentViewH = TabbarSafeBottomMargin + 54 * 2 + 8 + 250;
    contentView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, contentViewH);
    [contentView addRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight radius:20];
    _textView = textView;
    
    _checkButton = checkButton;
    _confirmButton = confirmButton;
    
    if (!_clockPlanId) {
        dispatch_async_sql_queue(^{
            TJUserModel *userModel = [TJUserModel currentAccountModel];
            dispatch_async_main_queue(^{
                textView.text = userModel.poiSourceInfo;
            });
        });
    }
}

- (void)textViewDidChange:(UITextView *)textView {
    _checkButton.enabled = !isEmptyString(textView.text);
}
 

- (BOOL)textViewShouldReturn:(UITextView *)textView {
    [textView resignFirstResponder];
    return YES;
}

- (void)didClickConfirmButton {
    [TJHUD showLoadingInView:self.view];
    NSString *text = _textView.text;

    [TJMinePresent setPOI:text clockPlanId:_clockPlanId complete:^(NSError * _Nullable error) {
        if (error) {
            return;
        }
        
        if (self.complete) {
            self.complete(text);
        }

        [self.view endEditing:YES];
        [self dismissComplete:^{
            [TJHUD showMessage:@"修改成功"];
        }];
    }];
}

- (void)didClickCheckButton {
    [self.view endEditing:YES];
    [TJHUD showLoadingInView:self.view];
    [TJMinePresent checkPoi:_textView.text clockPlanId:_clockPlanId complete:^(NSError * _Nullable error) {
        if (error) {
            self.confirmButton.enabled = NO;
            return;
        }
        
        self.confirmButton.enabled = YES;
        [TJHUD showMessage:@"校验成功"];
    }];
}

- (void)didClickDismissButton {
    [self dismissComplete:nil];
}

- (void)didClickMaskButton {
    [self.view endEditing:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView.tj_y = KScreenHeight - self.contentView.tj_height;
        self.maskView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    } completion:^(BOOL finished) {
    }];
    
}

- (void)dismissComplete:(void (^)(void))complete {
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
        self.contentView.tj_y = KScreenHeight;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (complete) {
                complete();
            }
        }];
    }];
}

- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationCustom;
}


@end

